package com.puzzlebazar.client.main.control;

import java.util.HashMap;

import com.puzzlebazar.client.main.view.MainView;
import com.puzzlebazar.client.main.view.TabbedView;
import com.puzzlebazar.client.user.UserModel;

public abstract class StateWithSubstates extends MainState {	

	protected final MySubStateTokenFactory subStateTokenFactory = new MySubStateTokenFactory();
	
	private static final String STATE = "State";

	protected final TabbedView tabbedView = new TabbedView();	
	
	public StateWithSubstates( UserModel model, MainView view, MainController controller ) {
		super( model, view, controller );		
	}

	/**
	 * @return The title to use for the main panel
	 */
	protected abstract String getMainPanelTitle();
	
	/**
	 * @return The subcontroller to control the substates
	 */
	protected abstract NamedStateController getSubController();
	
	/**
	 * Adds the state's custom parameters to the parameter hash table
	 * 
	 * @param params The parameter hash table to add to
	 */
	protected abstract void addCustomParams(HashMap<String, String> params);
	
	/**
	 * Get the current parameters of the state provided a puzzleId
	 * 
	 * @param stateName The name of the state for the subcontroller (null for default state)
	 * @return The parameters of the state
	 */
	public static HashMap<String,String> getParams( String stateName ) {
		final HashMap<String,String> params = new HashMap<String,String>();
		if( stateName != null )
			params.put( STATE, stateName );
		return params;
	}

	@Override
	public HashMap<String,String> getParams() {
		return getParams( getSubController().getCurrentStateName() );
	}

	/**
	 * Sets the parameters for the puzzle for which to get information
	 * The subclass is expected to call this after setting all its custom parameters 
	 * 
	 * @param stateName The state the subcontroller should be in (null for default state)
	 */
	protected void setParams( String stateName ) {
		if( stateName == null ) 
			getSubController().gotoDefaultState();
		else {
			try {
				getSubController().gotoStateNamed( stateName );
			} catch (Exception exception) {
				getSubController().gotoDefaultState();
				exception.printStackTrace();
			}
		}
		tabbedView.updateTokens();
	}

	@Override
	public void setParams( HashMap<String,String> params ) throws Exception {
		setParams( params.get( STATE ) );
	}


	@Override
	public void enter() {
		getSubController().enter();
		view.setMainPanelTitle( getMainPanelTitle() );
		view.setMainWidget( tabbedView );		
	}

	@Override
	public void exit() {
		getSubController().exit();
		view.setMainPanelTitle( null );
	}

	private class MySubStateTokenFactory implements SubStateTokenFactory {

		@Override
		public String buildToken(String subStateName) {
			HashMap<String, String> params = new HashMap<String, String>();
			addCustomParams(params);
			params.put(STATE, subStateName);	
			return controller.buildToken( getStateToken(), params );
		}
		
	}

	@Override
	public void release() {
		super.release();
		getSubController().release();
	}
	
}
